from pms.common.views import PostFormula
from project.models import Project
from project.utils.repository import project_add_repository, repository_view
from project.utils.permission import assert_project_permissions


class API(PostFormula):

    def handle(self, req):
        pid = req['pid']
        name = req['name']
        href = req['href']
        assert_project_permissions(pid, self.user.id, [2])
        return self.success_respond(repository_view(
            repository=project_add_repository(
                project=Project.objects.get(id=pid),
                name=name,
                href=href
            )
        ))
